﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 店铺管理
    /// </summary>
    [AppAuthorize("Member")]
    public class ShopInfoController : GeneralController {
        private IShopInfoService _iShopInfoService;
        private IMerchantService _iMerchantService;
        private IProductService _iProductService;
        private IRedRecordService _iRedRecordService;
        public ShopInfoController(IShopInfoService iShopInfoService, IMerchantService iMerchantService, IProductService iProductService, IRedRecordService iRedRecordService) {
            _iShopInfoService = iShopInfoService;
            _iMerchantService = iMerchantService;
            _iProductService = iProductService;
            _iRedRecordService = iRedRecordService;
        }
        /// <summary>
        /// 店铺管理
        /// </summary>
        /// <returns></returns>
        public ActionResult ShopManage() {
            var model = _iShopInfoService.GetShopById(UserId);
            ViewBag.img_url = RequestMethod.token_imgurl;
            if (model.id == 0) return RedirectToAction("Index");
            return View(model);
        }
        /// <summary>
        /// 店铺信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Index() {
            var model = _iShopInfoService.GetShopById(UserId);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(model);
        }
        /// <summary>
        /// 修改店铺信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult Save(shop_info_dto model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            string pathLogo = base.Upload("ShopLogo", "upload_shop_logo");
            if (!string.IsNullOrEmpty(pathLogo)) {
                model.shop_logo = pathLogo;
            } else {
                throw new ServerErrorException("请重新上传店铺logo");
            }
            if (!string.IsNullOrEmpty(model.shop_background_img)) {
                string pathBg = base.Upload("ShopBackImg", "upload_shop_bg");
                if (!string.IsNullOrEmpty(pathBg))
                    model.shop_background_img = pathBg;
                else throw new ServerErrorException("请重新上传背景图片");
            }
            model.id = UserId;
            var result = _iShopInfoService.Save(model);
            return Json(ResultToJson.Json(result ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 我的店铺信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult MyShopInfo(long id) {
            var model = _iShopInfoService.GetShopInfoById(id);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(model);
        }
        /// <summary>
        /// 我的店铺
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Myshop(long id) {
            var model = _iShopInfoService.GetShopInfoById(id);
            merchant_dto merchant = new merchant_dto();
            red_user_dto redcord = new red_user_dto();
            if (UserId > 0) {
                merchant = _iMerchantService.Get(UserId);
                redcord = _iRedRecordService.GetUser(UserId);
            }
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.merchant = merchant;
            ViewBag.redcord = redcord;
            ViewBag.shop_id = id;
            return View(model);
        }
        [AllowAnonymous]
        public PartialViewResult _Myshop(GoodsQuery query, long shop_id) {
            query.merchant_id = shop_id;
            var model = new GoodsQuery();
            model.page = query.page;
            model.size = query.size;
            model.sort = query.sort;
            model.keyword = query.keyword;
            model.merchant_id = query.merchant_id;
            model.sell_out = false;
            var list = _iProductService.GetShopGoods(model);
            var curMerchant = _iMerchantService.Get(UserId);
            ViewBag.UserType = curMerchant == null ? UserType.user : (UserType)curMerchant.usertype;
            ViewBag.img_url = RequestMethod.token_imgurl;
            ViewBag.shop_id = shop_id;
            ViewBag.share_no = curMerchant == null ? "" : curMerchant.merchant_no;
            return PartialView(list);
        }
        /// <summary>
        /// 节点商城
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult ShowShop() {
            if (UserId > 0) {
                var myshop = _iShopInfoService.GetShopById(UserId);
                if (myshop != null && myshop.id > 0) {
                    return Redirect("/DistributionMall/index2?shopId=" + myshop.id);
                }
                var merchants = _iMerchantService.GetParents(UserId);
                long shop_id = 0;
                if (merchants.Count() > 0) {
                    foreach (var item in merchants) {
                        if (item.id == UserId) {
                            if (item.is_supplier) {
                                var shop = _iShopInfoService.GetShopById(UserId);
                                if (shop.id > 0) {
                                    shop_id = item.id;
                                    break;
                                }
                            }
                        } else {
                            if (item.is_supplier) {
                                var shop = _iShopInfoService.GetShopById(item.id);
                                if (shop.id > 0) {
                                    shop_id = item.id;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (shop_id > 0) {
                    return Redirect("/DistributionMall/index2?shopId=" + shop_id);
                }
            }
            return RedirectToAction("Show", "ShopInfo");
        }
        /// <summary>
        /// 节点商城展示页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Show() {
            return View();
        }
        /// <summary>
        /// 跳转到完善店铺
        /// </summary>
        /// <returns></returns>
        public ActionResult GoToMyShop() {
            return View();
        }
    }
}